package com.woniuxy.hrms.dao.impl;

import com.woniuxy.hrms.dao.IEmployeeDao;
import com.woniuxy.hrms.entity.Employee;
import com.woniuxy.hrms.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @program: hrms
 * @description:
 * @create: 2024-08-08 17:29
 **/
public class IEmployeeDaoImplJdbc implements IEmployeeDao {
    Connection connection;
    PreparedStatement ps;
    ResultSet rs;
    String sql;

    @Override
    public int add(Employee employee) throws SQLException {
        connection = DBUtil.getCon();
        sql = "insert into employee(emp_id,emp_name,age,sex,phone,salary) values(?,?,?,?,?,?)";
        ps = connection.prepareStatement(sql);
        ps.setInt(1, employee.getId());
        ps.setString(2, employee.getName());
        ps.setInt(3, employee.getAge());
        ps.setString(4, employee.getSex());
        ps.setString(5, employee.getPhone());
        ps.setDouble(6, employee.getSalary());
        int i = ps.executeUpdate();
        DBUtil.release(connection, ps, rs);
        return i;
    }

    @Override
    public int delete(int id) throws SQLException {
        connection = DBUtil.getCon();
        sql = "delete from employee where emp_id=?";
        ps = connection.prepareStatement(sql);
        ps.setInt(1, id);
        int i = ps.executeUpdate();
        DBUtil.release(connection, ps, rs);
        return i;
    }

    @Override
    public int update(Employee employee) throws SQLException {
        connection = DBUtil.getCon();
        sql = "update employee set emp_name=?,age=?,sex=?,phone=?,salary=? where emp_id=?";
        ps = connection.prepareStatement(sql);
        ps.setString(1, employee.getName());
        ps.setInt(2, employee.getAge());
        ps.setString(3, employee.getSex());
        ps.setString(4, employee.getPhone());
        ps.setDouble(5, employee.getSalary());
        ps.setInt(6, employee.getId());
        int i = ps.executeUpdate();
        DBUtil.release(connection, ps, rs);
        return i;
    }

    @Override
    public List queryALL() {
        return null;
    }

    @Override
    public Employee queryByld(int id) {
        return null;
    }
}
